package com.karaku.karaku_collector.util.screenshot;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
import android.media.ImageReader;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.util.DisplayMetrics;

import com.karaku.karaku_collector.App;
import com.karaku.karaku_collector.util.UIUtil;
import com.karaku.karaku_collector.util.actions.Action1;
import com.karaku.karaku_collector.util.actions.Action2;

import java.nio.ByteBuffer;

/**
 * Created by guopi on 2017/8/22 0022.
 */

public class ScreenShotManager {

    public static void start(final Action1<Bitmap> callback) {
        if (callback == null) {
            return;
        }
        ScreenShotActivity.start(new Action2<Integer, Intent>() {
            @Override
            public void run(Integer result, Intent data) {
                if (result == Activity.RESULT_OK) {
                    capture(result, data, callback);
                } else {
                    callback.run(null);
                }
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static void capture(int result, Intent data, final Action1<Bitmap> callback) {
        final ImageReader reader = ImageReader.newInstance(UIUtil.getScreenSize().width, UIUtil.getScreenSize().height,
                PixelFormat.RGBA_8888, 2);
        MediaProjectionManager projectionManager =
                (MediaProjectionManager) App.getInstance().getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        final MediaProjection mProjection = projectionManager.getMediaProjection(result, data);
        final VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror",
                UIUtil.getScreenSize().width, UIUtil.getScreenSize().height,
                DisplayMetrics.DENSITY_MEDIUM, DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION,
                reader.getSurface(), null, null);

        reader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(final ImageReader reader) {
                ScreenShotManager.onImageAvailable(reader, callback);
                if (display != null) {
                    display.release();
                }
                reader.close();
            }
        }, null);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static void onImageAvailable(ImageReader reader, final Action1<Bitmap> callback) {
        Image image = reader.acquireLatestImage();
        int width = image.getWidth();
        int height = image.getHeight();
        final Image.Plane[] planes = image.getPlanes();
        final ByteBuffer buffer = planes[0].getBuffer();
        int pixelStride = planes[0].getPixelStride();
        int rowStride = planes[0].getRowStride();
        int rowPadding = rowStride - pixelStride * width;
        Bitmap bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888);
        bitmap.copyPixelsFromBuffer(buffer);
        Bitmap b = bitmap;
        bitmap = Bitmap.createBitmap(b, 0, 0, width, height);
        b.recycle();
        image.close();
        buffer.clear();
        callback.run(bitmap);
    }
}
